package com.cjm.one.controller.pay;

import com.cjm.one.common.result.Result;
import com.cjm.one.service.AliPayService;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@Slf4j
@Tag(name = "订单", description = "订单")
@RequestMapping("/api/pay/alipay")
public class AliPayController {
    @Resource
    private AliPayService aliPayService;
    /**
     * @description: 支付宝支付接口
     * @author: cjm
     * @date: 2025/3/31 22:52
     * @param: [orderNo, httpResponse]
     * @return: void
     **/
    @GetMapping("/pay")
    public void pay(@RequestParam("orderNo") String orderNo,HttpServletResponse response) throws IOException {
        log.info("订单号:{}", orderNo);
        aliPayService.alipay(orderNo,response);
    }

    /**
     * @description: 支付宝支付异步通知接口
     * @author: cjm
     * @date: 2025/3/31 22:53
     * @param: [request]
     * @return: java.lang.String
     **/
    @PostMapping("/notify")  // 注意这里必须是POST接口
    public Result<String> payNotify(HttpServletRequest request) throws Exception {
        // 提取支付宝回调参数
        Map<String, String> params = new HashMap<>();
        request.getParameterMap().forEach((key, values) ->
                params.put(key, values.length > 0 ? values[0] : "")
        );
        String str =  aliPayService.payNotify(params);
        return Result.ok(str);
    }
}
